<?php
/*
 * Author:YangBo Created on 2009-1-21:����01:21:58
 * 
 * TODO:TODO
 * as9
 * ClassService
 * 
 */
 require_once("./BaseService.php");
 require_once("./interfaces/IClassService.php");
 
 require_once("./dao/Class_.php");
 require_once("./util/ServiceUtil.php");
 class ClassService extends BaseService implements IClassService
 {
 	protected $class_;
 	public function __construct()
 	{
 		$this->initializeDB();
 		$this->class_ = new Class_();
 	}
 	/**
	 * 创建班级
	 * @param $target [175,url]
	 * @return:success
	*/
 	public function create($target)
 	{
 		$name = strtolower(trim($target[0]));
 		$description = strtolower(trim($target[1]));
 		
		try
		{
			$this->sql = "SELECT class.name FROM class WHERE class.name='$name'";
			$this->result = mysql_query($this->sql,$this->_db);
			$numRows = mysql_num_rows($this->result);
			if($numRows>=1)
			{
				return ServiceUtil::getFailureResult("failure","same class name existed!");
			}else
			{
				try
				{
					$this->sql = "INSERT INTO class(name,description) VALUES('$name','$description')";
					$this->result = mysql_query($this->sql,$this->_db);
					return ServiceUtil::getSuccessResult("success","create class success!");
				}catch(Exception $exc)
				{
					return $exc;
				}	
			}
		}catch(Exception $exc)
 		{
 			return $exc;
 		}
 	}
 	/**
	 * 读取班级
	 * @param $targetID 1
	 * @return:$this->class_
	*/
 	public function read($targetID)
 	{
 		if($targetID)
 		{
 			$cno = strtolower(trim($targetID));
 			try
	 		{
	 			$this->sql = "SELECT * FROM class WHERE cno='$cno'";
	 			$this->result = mysql_query($this->sql,$this->_db);
	 			
	 			while($row = mysql_fetch_object($this->result))
	 			{
	 				$this->class_->cno = $row->cno;
	 				$this->class_->name = $row->name;
	 				$this->class_->thumbnail = $row->thumbnail;
	 				$this->class_->created_time = $row->created_time;
	 				$this->class_->capcity = $row->capcity;
	 				$this->class_->user_registed = $row->user_registed;
	 				
	 				$this->ret[] = $this->class_;
	 			}
	 			return $this->ret;
	 		}catch(Exception $exc)
	 		{
	 			return $exc;
	 		}
 		}else
 		{
 			try
	 		{
	 			$this->sql = "SELECT * FROM class";
	 			$this->result = mysql_query($this->sql,$this->_db);
	 			$numRows = mysql_num_rows($this->result);
	 			for($i=0;$i<$numRows;$i++)
	 			{
	 				$row = mysql_fetch_object($this->result);
	 				$this->class_ = new Class_();
	 				$this->class_->cno = $row->cno;
	 				$this->class_->name = $row->name;
	 				$this->class_->thumbnail = $row->thumbnail;
	 				$this->class_->created_time = $row->created_time;
	 				$this->class_->capcity = $row->capcity;
	 				$this->class_->user_registed = $row->user_registed;
	 				
	 				$this->ret[] = $this->class_;
	 			}
	 			return $this->ret;
	 		}catch(Exception $exc)
	 		{
	 			return $exc;
	 		}
 		}
 		
 	}
 	/**
	 * 更新班级
	 * @param $target [1,test,url]
	 * @return:success
	*/
 	public function update($target)
 	{
 		$cno = strtolower(trim($target[0]));
 		$name = strtolower(trim($target[1]));
 		$thumbnail = strtolower(trim($target[2]));
 		
 		try
 		{
 			$this->sql = "UPDATE class SET name='$name',thumbnail='$thumbnail' WHERE cno='$cno'";
 			$this->result = mysql_query($this->sql,$this->_db);
 			return ServiceUtil::getSuccessResult("success","update class success!");
 		}catch(Exception $exc)
 		{
 			return $exc;
 		}
 	}
 	/**
	 * 删除班级
	 * @param $targetID 1
	 * @return:success
	*/
 	public function delete($targetID)
 	{
 		$cno = strtolower(trim($targetID));
 		try
 		{
 			$this->sql = "DELETE FROM class WHERE cno='$cno'";
 			$this->result = mysql_query($this->sql,$this->_db);
 			return ServiceUtil::getSuccessResult("success","delete class success!");
 		}catch(Exception $exc)
 		{
 			return $exc;
 		}
 	}
 	/**
	 * 注册用户数
	 * @param $cno 1
	 * @return:int
	*/
 	public function getUserRegisted($cno)
 	{
 		$myCno = strtolower(trim($cno));
 		try
 		{
 			$this->sql = "SELECT students.cno FROM students WHERE cno='$myCno'";
 			$this->result = mysql_query($this->sql,$this->_db);
 			$numRows = mysql_num_rows($this->result);
 			return $numRows;
 		}catch(Exception $exc)
 		{
 			return $exc;
 		}
 	}
 	
 	public function __destruct()
 	{
 		
 	}
 }
?>
